

let QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()
Page({

  data: {
    lineList: null,
    city: '',
    zhandian: '',//输入框的输入框的站点信息
  },

  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },

  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },

  getLocal: function (latitude, longitude) {
    let vm = this;
    var qqmapsdk = new QQMapWX({
      key: 'OMEBZ-DZY6G-HO4QU-IAJQG-GMTHQ-KKBHI'
    });
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log("获取地址：" + res.result.ad_info.city);
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        app.globalData.city = city;
        vm.data.city = city;
        console.log("city:" + city)

        vm.data.city = city;

      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },


  formSubmit: function (e) {
    var that = this;
    if (e.detail.value.zhandian == '') {
      wx.showToast({
        title: '请输入站点',
      })
    }
    console.log("站点：" + e.detail.value.zhandian)
    that.setData({
      zhandian:e.detail.value.zhandian
    })
    wx.request({//这里用了haoservice的公共交通路线接口
      url: 'http://apis.haoservice.com/lifeservice/busline/linename',
      data: {
        city: '广州市',
        keywords: e.detail.value.zhandian,
        // key: '45d049e919c84074b6f6288792f09ca0'
        key: '33a6cfbf944042439c99f8d77cc47002'//更换新的api，之前白嫖的过期了
      },
      success: function (r) {
        console.log("公交：" + JSON.stringify(r.data))
        if (r.data.result.buslines.length > 0) {
          that.setData({
            line: r.data.result.buslines,
          })
          //同时数据也扔到全局去吧，其他界面也是需要使用的
          app.globalData.buslines = r.data.result.buslines;
        }



      }
    })
  },

  //添加一个用map显示线路图的相关功能
  //首先要想想，需要用到的数据，
  //1 ,每个公交站点的详细信息，这个直接使用haoservice的“公交线路”查询接口即可，免费又好用
  //2 ,公交车的实时位置，这个要是用笑园实时公交api的“实时公交”接口，但是这个接口要求信息有点多，要:城市id，线路id，这两个都要依靠笑园的其他接口，（真是会赚钱，为了提升接口使用量）
  toMapShowRoute(event){
    let that = this;
    let idx = event.currentTarget.dataset.index;
    console.log("当前点击的线路顺序：",idx);
    console.log("线路数据:",that.data.line[idx]);
    wx.navigateTo({
      url: '../shishiLine/index?idx='+idx+'&zhandian='+that.data.zhandian,
    })
  },




  onLoad: function (options) {

    var that = this;
    var qqmapsdk = new QQMapWX({
      key: 'OMEBZ-DZY6G-HO4QU-IAJQG-GMTHQ-KKBHI'
    });
    that.getUserLocation();



  },

  onReady: function () { },

  onShow: function () {
    var that = this

  },



  onHide: function () { },

  onUnload: function () { },

  onPullDownRefresh: function () { },

  onReachBottom: function () { },

  onShareAppMessage: function () { },



});